<?php 

class Zen_Form_Element_Factory {
	
	private static $_field_name=null;
	
	private static $_label=null;
	
	private static $_name=null;
	
	private static $_description=null;
	
	private static $_require=false;
	
	public static function get ( $name, $require=null, $label=null, $field_name=null, $description=null) {
		self::$_name = $name;
		self::$_label = $label;
		self::$_require = $require;
		self::$_field_name = $field_name;
		self::$_description = $description;
		self::_init();
		$method = '_get' . self::$_name;
		return self::$method();
	}
	
	public static function getSelect( $name, $require=false, $label=null, $list=array(), $first='-', $field_name=null, $description=null) {
		self::$_name = $name;
		self::$_label = $label;
		self::$_require = $require;
		self::$_field_name = $field_name;
		self::$_description = $description;
		self::_init();
        $select = new Zend_Form_Element_Select ( self::$_field_name, 
        	array ( 'required' => self::getElementRequire(false) , 
        			'label' => self::getElementLabel('') . ':',
        			'maxlength' => '300', 
        			'description' =>self::getElementDescription(null),
        			'class' => 'zend_text' ) 
        	);		
        if ($first)	$list [0] = $first;
		$select->setMultiOptions ( $list );
		$select->setValue ( 0 );
		return $select;
	}
	
	public static function getMultiSelect( $name, $require=false, $label=null, $list=array(), $field_name=null, $description=null) {
		self::$_name = $name;
		self::$_label = $label;
		self::$_require = $require;
		self::$_field_name = $field_name;
		self::$_description = $description;
		self::_init();
        $select = new Zend_Form_Element_Multiselect ( self::$_field_name, 
        	array ( 'required' => self::getElementRequire(false) , 
        			'label' => self::getElementLabel('') . ':',
        			'maxlength' => '300', 
        			'description' =>self::getElementDescription(null),
        			'class' => 'zend_text' ) 
        	);		
       	$select->setMultiOptions ( $list );
		$select->setValue ( 0 );
		return $select;
	}
	
	public static function getRadio( $require=false, $label=null, $list=array(), $field_name=null, $description=null) {
		self::$_name = 'radio';
		self::$_label = $label;
		self::$_require = $require;
		self::$_field_name = $field_name;
		self::$_description = $description;
		self::_init();
        $radio = new Zend_Form_Element_Radio ( self::$_field_name, 
        	array ( 'required' => self::getElementRequire(false) , 
        			'label' => self::getElementLabel('') . ':',
        			'maxlength' => '300', 
        			'description' =>self::getElementDescription(null),
        			'class' => 'zend_text' ) 
        	);		
       	$radio->setMultiOptions ( $list )
				->setValue ( 0 )
				->setSeparator('&nbsp;&nbsp;')
				;
		return $radio;
	}
	
	public static function getSubForm ($label, $container_id) {
		$subForm = new ZendX_JQuery_Form();
		$subForm->setDecorators(array(
    	'FormElements',
    	array('HtmlTag',
        	  array('tag' => 'dl','class'=>'zend_form')
        	  ),
    	array('TabPane',
          array('jQueryParams' => array('containerId' => $container_id,
                                        'title' => $label)))
		));
		return $subForm;
	}
	
	private static function _init() {
		if (! self::$_name) throw new Exception('There is no name defined');
		if (! self::$_field_name ) {
			self::$_field_name = strtolower( self::$_name );
		}
		self::$_name = ucfirst (self::$_name);
	}
	
	private static function getElementLabel ($default) {
		return ( self::$_label ) ? self::$_label : $default;
	}
	
	private static function getElementRequire ($default) {
		return ( self::$_require !== null ) ? self::$_require : $default;
	}
	
	private static function getElementDescription ($default) {
		return ( self::$_description !== null ) ? self::$_description : $default;
	}
	
	private static function _getLabel () {
		$label = new Zend_Form_Element_Text( self::$_field_name ,
        array('required' => self::getElementRequire(true) ,
          'label' => self::getElementLabel('Название') . ':', 
          'maxlength' => '300', 
          'class'=>'zend_text',
          'description' =>self::getElementDescription(null),
          'validators' => array(array('StringLength' , true , array(0 , 300 , 'utf-8')))));
        $label->setFilters ( array ('StringTrim', 'StripTags' ));
        return $label;
	}
	
	private static function _getEmail () {
		$email = new Zend_Form_Element_Text(self::$_field_name,
        array('required' => self::getElementRequire(true) ,
          'label' => self::getElementLabel('Email') . ':',
          'class'=>'zend_text',
         ));
        $email->setFilters ( array ('StringTrim', 'StripTags' ));
        $email->addValidator('EmailAddress');
        return $email;
	}
	
	
	private static function _getFile () {
		$file = new Zend_Form_Element_File(self::$_field_name);
        $file->setLabel(self::getElementLabel('Прикрепить файл'))
            ->setDestination(ROOT_PATH . '/tmp')
            ->setRequired(self::getElementRequire(false))
            ->setAttrib('size','36');
        return $file;
	}
	
	private static function _getText () {
		$text = new Zen_Form_Element_Tiny ( self::$_field_name , 
        	array ('required' => self::getElementRequire(true), 
        		'label' =>  self::getElementLabel('Текст') . ':', 
        		'rows' => '10', 'cols' => '30',
        		'description' =>self::getElementDescription(null),
           ));
        return $text;
	}
	
	private static function _getTextarea () {
		$text = new Zend_Form_Element_Textarea ( self::$_field_name , 
        	array ('required' => self::getElementRequire(true), 
        		'label' =>  self::getElementLabel('Текст') . ':', 
        		'class'=>'zend_text_area',
        		'rows' => '10', 'cols' => '30',
        		'description' =>self::getElementDescription(null),
           ));
        return $text;
	}
	
	private static function _getImages () {
		$images = new Zen_Form_Element_Imageloader(self::$_field_name,
	        array('required' => self::getElementRequire(false),
	          	'label' => self::getElementLabel('Изображение') . ':', 
	        	'class'=>'zenimages',
	        	'description' =>self::getElementDescription(null),
	        ));
        $images->setFilters ( array ('StringTrim','StripTags'));
        return $images;
	}
	
	private static function _getAlias () {
		$alias = new Zend_Form_Element_Text(self::$_field_name,
	        array(
	          'required' => self::getElementRequire(false) ,
	          'label' => self::getElementLabel('Псевдоним') . ':', 
	          'maxlength' => '300', 
	          'class'=>'zend_text',
	          'description' =>self::getElementDescription(null),
	        ));
        $alias->setFilters ( array ('StringTrim', 'Alias'));
        return $alias;
	}
	
	private static function _getHidden () {
		$hidden = new Zend_Form_Element_Hidden(self::$_field_name,
	        array(
	          'required' => self::getElementRequire(false) ,
	        ));
        $hidden->setFilters ( array ('StringTrim', 'StripTags'));
        $hidden->removeDecorator('label');
        return $hidden;
	}
	
	private static function _getActive () {
		$active = new Zend_Form_Element_Radio(self::$_field_name,
	        array('required' => self::getElementRequire(true) ,
	          'label' => self::getElementLabel('Активен') . ':',
	          'description' =>self::getElementDescription('Устанавливает видимость элемента на сайте'),
	          ));
	    $active->setMultiOptions(array('1'=>'Да','0'=>'Нет'));
		$active->setFilters ( array ('StringTrim', 'StripTags' ));
        $active->setValue(1);
        return $active;
	}
	
	private static function _getWeight () {
		$weight = new Zend_Form_Element_Text(self::$_field_name,
	        array('required' => self::getElementRequire(false),
	          	'label' => self::getElementLabel('Вес') . ':', 
	          	'class'=>'zend_text',
	          	'description' =>self::getElementDescription('Чем больше значение веса, тем вышеэлемент на странице'),
	            ));
	        $weight->setFilters ( array ('StringTrim', 'StripTags' ));
	        $weight->setValidators( array ('Int'));
        return $weight;
	}
	
	private static function _getDate () {
		$date = new ZendX_JQuery_Form_Element_DatePicker(
                    self::$_field_name,
                    array(
                    'required' => self::getElementRequire(true) ,
                    'validators' => array(
                                array('StringLength' , true , array(6 , 11 , 'utf-8')),
                                array('Date',true,array('yyyy-MM-dd'))
                                ),
                    'label'=>self::getElementLabel('Дата') . ':', 
                    'jQueryParams' => array('dateFormat'=>'yy-mm-dd'),
                    'description' =>self::getElementDescription(null),
                    )
                );
        $date->setFilters ( array ('StringTrim', 'StripTags'));
        $date->setValue(date('Y-m-d'));
        return $date;
	}
	
	private static function _getDateTime () {
		$date = new Zen_Form_Element_DateTime(
                    self::$_field_name,
                    array(
                    'required' => self::getElementRequire(true) ,
                    'validators' => array(
                                array('StringLength' , true , array(6 , 19 , 'utf-8')),
                                array('Date',true,array('yyyy-MM-dd'))
                                ),
                    'label'=>self::getElementLabel('Дата') . ':', 
                    'jQueryParams' => array('dateFormat'=>'yy-mm-dd', 'timeFormat'=>'HH:mm:ss'),
                    'description' =>self::getElementDescription(null),
                    )
                );
        $date->setFilters ( array ('StringTrim', 'StripTags'));
        return $date;
	}
	
	private static function _getPreview () {
		$preview = new Zend_Form_Element_Textarea ( self::$_field_name, 
        	array ('required' => self::getElementRequire(true), 
        		'label' => self::getElementLabel('Краткое содержание новости') . ':',
        		'rows' => '5', 'cols' => '30',
        		'class' => 'zend_text_area', 
        		'description' =>self::getElementDescription(null),
           ));
        $preview->setFilters ( array ('StringTrim', 'StripTags'));
        return $preview;
	}
	
	private static function _getVideo () {
		$video = new Zend_Form_Element_Textarea ( self::$_field_name, 
        	array ('required' => self::getElementRequire(false), 
        		'label' => self::getElementLabel('Embedded код видео') . ':', 
        		'rows' => '5', 'cols' => '30',
        		'class' => 'zend_text_area', 
        		'description' =>self::getElementDescription(null),
           ));
        $video->setFilters ( array ('StringTrim'));
        return $video;
	}
	
	private static function _getMainpage () {
		$mainpage = new Zend_Form_Element_Radio( self::$_field_name,
        array('required' => self::getElementRequire(true) ,
          	'label' => self::getElementLabel('На главной') . ':',
        	'description' =>self::getElementDescription(null), 
          ));
        $mainpage->setMultiOptions(array('1'=>'Да','0'=>'Нет'));
		$mainpage->setFilters ( array ('StringTrim', 'StripTags' ));
        $mainpage->setValue(0);
        return $mainpage;
	}
	
	private static function _getPrice () {
		$validator = new Zend_Validate_Float(new Zend_Locale('en'));
        $price = new Zend_Form_Element_Text(self::$_field_name,
        array(
        	'required' => self::getElementRequire(false) ,
          	'label' => self::getElementLabel('Цена') . ':', 
          	'class'=>'zend_text',
        	'description' =>self::getElementDescription(null),
            ));
		$price->setFilters ( array ('StringTrim', 'StripTags' ));
        $price->addValidator($validator);
        $price->setValue(0);
        return $price;
	}
	
	private static function _getSubmit () {
		$sumbit = new Zend_Form_Element_Submit(self::$_field_name,
			array(
				'required' => self::getElementRequire(true) ,
				'label'=> self::getElementLabel('Сохранить'),
				'class'=>'btn btn-submit',
			)
		);
		$sumbit->removeDecorator('label');
		return $sumbit;
	}
	
	private static function _getNote () {
		$note = new Zen_Form_Element_Note(self::$_field_name,
			array(
				'required' => self::getElementRequire(true) ,
			)
		);
		$note->removeDecorator('label');
		return $note;
	}
	
	private static function _getAutocomplete () {
		$autocomplete = new Zen_Form_Element_Autocomplete(
        	'compare',
        	array(
        		'required' => self::getElementRequire(false),
	          	'label' => self::getElementLabel(''),
	        	'class' => 'zend_text',
        		'multiple' => false,
        		'jQueryParams' => array(
        			'minLength' => 3,
        			'source' => ''
        		),
	         )
        );
        $autocomplete->setFilters ( array ('StringTrim', 'StripTags'));
        return $autocomplete;
	}
	
	private static function _getElfinder () {
		$file_src = new Zen_Form_Element_Elfinder('file_src',
        	array(
	        	'required' => self::getElementRequire(false),
	          	'label' => self::getElementLabel('Файл'),
	        )
        );
        $file_src->setFilters ( array ('StringTrim' ));
        return $file_src;
	}
		
}